.NET Forum / Languages / C# / September 2007
How to specify COMPRESSION level when saving (loseless) PNGs ?
|
|
Thread rating:  |
pamela fluente - 29 Aug 2007 19:20 GMT I have been using something like:
public void SaveJPG(Image Image, string FileName, long QualityLevel_0_100, long ColorDepthLevel) { ImageCodecInfo ImageCodecInfoJPG = GetEncoderInfo("image/jpeg"); EncoderParameters EP = new EncoderParameters(2); EP.Param(0) = new EncoderParameter(Encoder.Quality, QualityLevel_0_100); EP.Param(1) = new EncoderParameter(Encoder.ColorDepth, ColorDepthLevel); Image.Save(FileName, ImageCodecInfoJPG, EP); }
to save JPG with some give given quality. I expected to do the same with PNG. I know its a loseless format, but I expect to be able to control the compression level. Is that right or no ?
My PNG are ways too big (relative to similar jpg's). Can anyone tell me what am I doing wrong. The following code (invented) is not working. What's the right way to do it?
public void SavePNG(Image Image, string FileName, long QualityLevel_0_100, long ColorDepthLevel) { ImageCodecInfo ImageCodecInfoPNG = GetEncoderInfo("image/png");
EncoderParameters EP = new EncoderParameters(2); EP.Param(0) = new EncoderParameter(Encoder.Compression, 100l - QualityLevel_0_100); EP.Param(1) = new EncoderParameter(Encoder.ColorDepth, ColorDepthLevel); Image.Save(FileName, ImageCodecInfoPNG, EP); }
-P
pedrito - 29 Aug 2007 19:48 GMT >I have been using something like: > [quoted text clipped - 13 lines] > with PNG. I know its a loseless format, but I expect to be able to > control the compression level. Is that right or no ? No
> My PNG are ways too big (relative to similar jpg's). Can anyone tell > me what am I doing wrong. The following code (invented) is not > working. What's the right way to do it? Your PNGs are so big because nothing is LOST in the compression. That's why Jpeg is able to compress so well. Specifying the amount of information you're willing to LOSE for a smaller size, gives it a lot more flexibility.
Just out of curiousity, what would you think the "compression level" you want out of PNG to do? It can't use less information than the exact colors of every pixel compressed as well as its algorithm allows. So what else is there for it to trade off?
Jon Skeet [C# MVP] - 29 Aug 2007 20:03 GMT <snip>
> Just out of curiousity, what would you think the "compression level" you > want out of PNG to do? It can't use less information than the exact colors > of every pixel compressed as well as its algorithm allows. So what else is > there for it to trade off? Well, potentially time spent doing compression. Most zip tools allow you to express a preference as to how "hard" you want them to try to compress, but it's still lossless.
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
pamela fluente - 31 Aug 2007 10:05 GMT > >I have been using something like: > [quoted text clipped - 30 lines] > > - Mostra testo tra virgolette - I do not know exactly. I have seen that usually when there is a "compression", one can specify a "compression level". So I was wondering whether there are parameters that need to be adjusted to control somehow the size. I could not find them
-P
Peter Duniho - 29 Aug 2007 19:59 GMT > [...] > to save JPG with some give given quality. I expected to do the same > with PNG. I know its a loseless format, but I expect to be able to > control the compression level. Is that right or no ? > > My PNG are ways too big (relative to similar jpg's). You should not expect to be able to control the compression level in the way that you are doing.
PNG does support a variety of parameters for controlling how the compression is done. But different parameters will result in better results for different input images.
JPEG's compression level is essentially a control over how much information to throw away. Higher quality discards less information, lower quality discards more. The more information you throw out, the smaller the file. .NET provides a simple 0 to 100 parameter to control how much information is discarded.
PNG's parameters (assuming you have an implementation that provides access to them) control varying ways that the algorithm can deal with the input data. A given set of parameters will not always produce the smallest files; for a given input image, the smallest file is obtained with a specific set of parameters, but a different input image may require a different set of parameters.
The only way to optimize (make the smallest) the output of PNG compression is to try a wide variety of parameters and pick the best results. This means compressing the image multiple times, and depending on how optimal you want to get, the number of times could be very large.
You should not, in any case, expect the size of a PNG-compressed file to approximate the size of a JPEG-compressed file. The two algorithms serve very different purposes, and aren't really comparable. JPEG throws out data, which makes it a lot easier to get very small files. But because it throws out data, it works best with images for which you won't notice the loss of data, like photographs or elaborate drawings.
For basic images, like bitmaps with text or simple drawings, compressing using JPEG can produce very unlikable results. The image will have "artifacts" all over it, causing clean lines to get blurry or distorted. On the other hand, for simple images like this, PNG's compression, even a default mode, can often do a pretty good job.
Whether it can do a better job than JPEG depends a lot on the input data and how you are using each compression technique. But generally speaking, JPEG isn't going to provide anywhere near the equivalent quality for simple images like those PNG is well-suited for, especially when it's used in a way that produces a file significantly smaller than PNG.
Now, all that said...as far as I know, .NET's support for PNG offers basically no options for controlling the parameters for PNG. So you don't really have a choice, if you're going to use .NET's PNG support. There are other options though.
Here's a command-line utility that does allow for control of various parameters: http://advsys.net/ken/util/pngout.htm
Here's a .NET program that uses that tool, but via a GUI: http://brh.numbera.com/software/pnggauntlet/
Here's a web site full of useful information about PNG: http://www.libpng.org/pub/png/
Here's that web site's introduction of PNG to beginners: http://www.libpng.org/pub/png/pngintro.html
Hope that helps.
Pete
pamela fluente - 31 Aug 2007 10:09 GMT > > [...] > > to save JPG with some give given quality. I expected to do the same [quoted text clipped - 64 lines] > > Pete Tha's all very interesting. But back to my specific issue, what about within .NET ?
Can I adjust the encoder parameters? And if yes, how? Is there any example ?
-P
Peter Duniho - 31 Aug 2007 17:11 GMT > Tha's all very interesting. But back to my specific issue, what about > within .NET ? > > Can I adjust the encoder parameters? And if yes, how? Is there any > example ? As I wrote in my post, which you quoted in its entirety:
>> Now, all that said...as far as I know, .NET's support for PNG offers >> basically no options for controlling the parameters for PNG. So you >> don't really have a choice, if you're going to use .NET's PNG support. Was there something about that statement that confused you?
pamela fluente - 03 Sep 2007 14:25 GMT > > Tha's all very interesting. But back to my specific issue, what about > > within .NET ? [quoted text clipped - 9 lines] > > Was there something about that statement that confused you? Yes I understood that. But was a kind of denial :-)
Just wanted to make sure whether, shamefully, there is NO HOPE for .NET programmers :-((( to output more reasonable PNGs.
-P :-)) Thanks a lot !
Peter Duniho - 03 Sep 2007 18:20 GMT > Just wanted to make sure whether, shamefully, there is NO HOPE for > ..NET programmers :-((( to output more reasonable PNGs. Define "more reasonable". Your initial post carries indications that you have unreasonable expectations for PNG. No matter what compression settings you use, it is simply not going to come close to JPEG in terms to size reduction. It's a completely different kind of compression, intended for different goals.
Likewise, while you can optimize PNG performance through adjustment of its parameters, for most general-purpose images you are not going to achieve massive differences depending on the parameters, even if you could control them via .NET. 5-10% differences are likely more typical, though of course there are specific examples that would be much more dramatic than that.
Now, if after all that, you really still want to be able to control the PNG compression parameters, you should not feel like you are limited to the functionality that the .NET Framework provides. Just because .NET doesn't provide native support for PNG compression options, that doesn't mean you cannot provide your own.
One of the things I find most appealing about the PNG format is that it's completely open and free of royalty encumberances. You can go to the main PNG web site, get the spec and implement it yourself based on that specification and the zlib specification.
Harder? Yes, no doubt. Far from impossible though. There are even open existing implementations on which you can base your implementation, depending on whether you're able to incorporate their licensing requirements into your own.
Or you could just use an existing implementation via p/invoke from within your .NET program.
Heck, you might even be able to find a PNG implementation provided as a managed .NET DLL.
I think you are far from being out of options.
Pete
pamela fluente - 03 Sep 2007 19:09 GMT > > Just wanted to make sure whether, shamefully, there is NO HOPE for > > ..NET programmers :-((( to output more reasonable PNGs. [quoted text clipped - 37 lines] > > Pete Thanks a lot.
I'll give it a try when I have time. I am curious to dig into the png format !
-P
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|