Skip to main content

Quality setting for PNG images

18 replies [Last post]
saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

Hello,

I am a newbee. Basically I am a coldfusion programmer. For last 20 days I have been working on java. Everything seemed to go smoothly. But I have got stuck while working with Image Libraries.

I need to set the Compression Quality for PNG images & JPEG images. There was no problem while doing it for JPEG images. I used JPEGImageWriteParam for this purpose. It is not possible for me to use ImageWriteParam for this purpose as ImageWriteParam doesn't support PNG format for compression related operations.

I managed to know a little about JAI Image output tools. Now I know it can be done using CLibPNGImageWriteParam. But I am not able to define any parameters using this.

I am stuck at very start where I create an instance of CLibPNGImageWriteParam. This class doesn't seem to be public. That's why it is not letting me create it's object.

I used the following line of code to create the object of JPEGImageWriteParam.

JPEGImageWriteParam jpegparams = new JPEGImageWriteParam( null );

But the similar one I am using for CLibPNGImageWriteParam is not working.

pngparams = new CLibPNGImageWriteParam( null );

Following is the error message that I receive when I try to compile the code.

PresentationTest5.java:61: com.sun.media.imageioimpl.plugins.png.CLibPNGImageWri
teParam is not public in com.sun.media.imageioimpl.plugins.png; cannot be access
ed from outside package
CLibPNGImageWriteParam pngparams;
^

PresentationTest5.java:169: com.sun.media.imageioimpl.plugins.png.CLibPNGImageWr
iteParam is not public in com.sun.media.imageioimpl.plugins.png; cannot be acces
sed from outside package
pngparams = new CLibPNGImageWriteParam( null );
^

I desperatly need this to work. Any help is appreciated. If needed I can even post the entire Java code. Please let me know If this helps anybody in solving my problem.

Many many Thanks in advance.
Deep

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
James Cheng

> The sample PNG file that I am using is 122 KB. But after changing the
> quality parameter to 0.1F the file size reached 2.19 MB, even setting
> the quality parameter to 0.9F gives the same file size as 0.1F. So,
> apparantly quality setting is not working and file size is getting
> increased many a times. On my side I have confirmed that the writer
> that is in use is CLibPNGImageWriter only.

As Robert pointed out, make sure that you don't convert a b&w image to
a full color image before writing. See the following page for how to
write a 1-bit image in PNG format:
http://download.java.net/media/jai-imageio/javadoc/1.1-latest/overview-s...

As for the same file size for different quality parameters, I wonder
whether you truncated the output file before writing to it. If not,
you might have seen something like this: overwriting a file with a
smaller file retains the old file's size. One of the following may
cure the problem:

aRandomAccessFile.setLength(0);
aRandomAccessFile.getChannel.truncate(0);
aFileOutputStream.getChannel.truncate(0);

-James

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

Sorry guys,

I was out of business for quite a few days due to some health related problems. Hence my problem with PNG quality settings stuck where you guys gave me a few ideas the last time.

I tried using the different PNG image. The variation in file size isn't like before. The file size surely changes when I change the quality settings. But still when I reduce the quality the file size gets increased. The original file size is 15.8 KB. When I compress it with quality setting 0.5F, the file size reaches to 18.6 KB. Even for the worst quality i.e. 0.9F, it reaches 18.2 KB. And for Best quality i.e. 0.1F it reaches 49 KB. What should happen ideally is that when I reduce the quality the file size should decrease, but it's not happening.

I am not sure whether the PNG image I am using is a low bit image image or not. The image is not B&W. I can see a few colors in it. It is a 8 bit image. The previous one was a 24 bit image. But for one thing I am sure that I am not saving the image to full color blindly. I am using the getColorModel() and getSampleModel() of the BufferedImage only to create the ImageTypeSpecifier. As you can find in the following code.

bufferedImage = ImageIO.read(new File("c:\\test\\C.png"));

writers = ImageIO.getImageWritersByFormatName( "png" );

imageWriter = (ImageWriter) writers.next();

pngparams = imageWriter.getDefaultWriteParam();
pngparams.setCompressionMode( pngparams.MODE_EXPLICIT );
pngparams.setCompressionQuality( 0.5F );
pngparams.setProgressiveMode( pngparams.MODE_DISABLED );
pngparams.setDestinationType(
new ImageTypeSpecifier( bufferedImage.getColorModel(), bufferedImage.getSampleModel() ) );

Any ideas what's happening or how should I overcome this?

Looking forward to your response.

Thank You
Deep

> > The sample PNG file that I am using is 122 KB. But
> after changing the
> > quality parameter to 0.1F the file size reached
> 2.19 MB, even setting
> > the quality parameter to 0.9F gives the same file
> size as 0.1F. So,
> > apparantly quality setting is not working and file
> size is getting
> > increased many a times. On my side I have confirmed
> that the writer
> > that is in use is CLibPNGImageWriter only.
>
> As Robert pointed out, make sure that you don't
> convert a b&w image to
> a full color image before writing. See the following
> page for how to
> write a 1-bit image in PNG format:
> http://download.java.net/media/jai-imageio/javadoc/1.1
> -latest/overview-summary.html#PNG
>
> As for the same file size for different quality
> parameters, I wonder
> whether you truncated the output file before writing
> to it. If not,
> you might have seen something like this: overwriting
> a file with a
> smaller file retains the old file's size. One of the
> following may
> cure the problem:
>
> aRandomAccessFile.setLength(0);
> aRandomAccessFile.getChannel.truncate(0);
> aFileOutputStream.getChannel.truncate(0);
>
> -James
>
> ------------------------------------------------------
> ---------------
> To unsubscribe, e-mail:
> interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail:
> interest-help@jai-imageio.dev.java.net

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

James, Brian, Robert !!

Any ideas or any kind of help? I really need it.

Eagerly waiting for your response.

Deep

robert engels

It is my understanding that PNG does not support "lossy" compression,
so changing the "quality" parameter does not make sense. (There is some

JPEG supports lossy compression.

"JPEG in TIFF" supports lossy compression.

It is very possible that it is impossible to have a PNG is smaller
than the comparable JPEG.

Here is a link that might explain things better for you: http://
mindprod.com/jgloss/png.html

Much more detailed information is available at http://www.libpng.org/
pub/png/pngintro.html

Some info from the FAQ

# Q: Why does your web site say PNGs are smaller than GIFs? All of
mine are bigger!

A: There are two main reasons behind this phenomenon: comparing
apples and oranges (that is, not comparing the same image types), and
using bad tools.

A common user mistake is to start with a truecolor image, save it in
both PNG and GIF format, and then compare. Unlike GIF, which supports
only colormapped (paletted) images, PNG supports both colormapped and
truecolor images--and not just at 24 bits; the latter can be 48 bits,
too. Because of this, when saving a truecolor (24-bit) image as a
GIF, most tools will happily throw away color information in order to
squeeze the image down to 8 bits (the largest possible palette size).
But with PNGs, there's no need to be so brutal; tools almost
invariably save such images in their full 24-bit glory. PNG's
compression engine may be efficient, but there's no possible way it
can make up for the initial factor-of-three handicap. To do a fair
test, save as GIF first, then open that image and save it as a PNG.
Now the PNG will be (or should be) the same bit depth as the GIF, and
in most cases it will be noticeably smaller.

The other reason is more of a developer issue, although users should
be aware of it. PNG provides a lot of flexibility in tuning the
compression level of an image, from trimming the color and
transparency palettes to choosing the precise combination of pre-
compression filters to choosing the proper settings for the
compression engine itself. (GIF, on the other hand, is practically
deterministic; aside from rearranging the order of the palette--an
extremely time-consuming and rare optimization--or turning off
compression altogether, you get pretty much the same results
regardless of the tool you use.) Not all tools do a good job of
compressing PNG images, and in extreme cases the difference can be as
large as a factor of two. Photoshop has traditionally been the poster
child for poor PNG implementations, but in fairness, recent releases
have also included ImageReady, an optimizer that does a better job.
See the basic introduction and and image editors pages for details.
For serious (yet completely lossless) compression, check out
pngcrush, pngrewrite, PNGOUT, and OptiPNG on the image converters page.

A side note: for all practical purposes, PNG is never smaller than
JPEG for photographic images. On the other hand, for buttons and
simple graphics with relatively few colors, PNG usually is smaller
than JPEG. Use the right tool for the job! Again, see the basic
introduction for details.

Robert

On Jul 11, 2006, at 3:13 AM, jai-imageio@javadesktop.org wrote:

> James, Brian, Robert !!
>
> Any ideas or any kind of help? I really need it.
>
> Eagerly waiting for your response.
>
> Deep
> [Message sent by forum member 'saurabhdeep' (saurabhdeep)]
>
> http://forums.java.net/jive/thread.jspa?messageID=131459
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>

[att1.html]

James Cheng

The PNG writer in JAI-ImageIO uses the quality setting
to control the zlib compression level, so you could get
different file sizes with different quality settings.
The zlib compression is lossless, and the compression
level is a trade-off between compression/decompression
time and file size. Since it's lossless, the file size
changes in a range much less than that of lossy JPEG.

As for why the output PNG file was bigger than the
input PNG file, there could be several reasons, such as
different default parameters, metadata, and etc. If
possible, please file an issue with a test case, so
that we can track and investigate it.

Thanks,
-James

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

> The PNG writer in JAI-ImageIO uses the quality
> setting
> to control the zlib compression level, so you could
> get
> different file sizes with different quality settings.
> The zlib compression is lossless, and the compression
> level is a trade-off between
> compression/decompression
> time and file size. Since it's lossless, the file
> size
> changes in a range much less than that of lossy JPEG.
>
> As for why the output PNG file was bigger than the
> input PNG file, there could be several reasons, such
> as
> different default parameters, metadata, and etc. If
> possible, please file an issue with a test case, so
> that we can track and investigate it.
>
> Thanks,
> -James
>

Hi James,

I have no idea of how to file an issue with test case. Can you please guide me in the right direction?

Should I post the contents of my Java file? Or is there any process to attach the files with the issue. I will also attach the PMG image that I am using in that case.

Sorry for bugging you so much. :(

Thanks
Deep

James Cheng

Hi Deep,

> I have no idea of how to file an issue with test case. Can you please
> guide me in the right direction?
>
> Should I post the contents of my Java file? Or is there any process
> to attach the files with the issue. I will also attach the PMG image
> that I am using in that case.

You can go to

https://jai-imageio-core.dev.java.net/servlets/ProjectIssues

and pick one item under "Enter an issue", probably "Enhancement"
for this case.

You need to register to become a member and then login in order
to be able to log an issue. Once you have entered an issue, you
can attach source code and image files, as long as it's okay for
you to show them to the public. A complete, standalone, buildable
(i.e., no need to edit) is highly appreciated.

More info can be found at:

https://www.dev.java.net/scdocs/ddIssues_EnterModify

Thanks,
-James

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

Hi James,

I have posted the issue there. A have also attached the Java code and the PNG image there. Java code will work as it is. It won't need any editing. I have taken care of that. I have also included some comments to the code so that it doesn't look messy out there.

I think, now I will have to wait and watch for the outcome of this.

Thanks a lot to you guys for spendig your invaluable time and sharing your knowledge with a newbee like me.

I will get in touch with you guys if I find anything somewhere else.

Warm Regards
Deep

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

Hi James,

As you already know, I had posted this issue with Source Code and PNG Image to the jai-imageio-core Issue List. But no one seems to be responding on the same.

How should I get rid of this problem. Can you please help me out on this.

Eagerly waiting for your reply.

Thanks in Advance
Deep

James Cheng

Hi Deep,

> As you already know, I had posted this issue with Source Code and PNG Image to the jai-imageio-core Issue List. But no one seems to be responding on the same.
>
> How should I get rid of this problem. Can you please help me out on this.

First, thanks for filing the issue. And sorry for that we haven't been
able to investigate it sooner. I am going to take a look at it shortly.

Thanks,
-James

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

Hi James,

Thanks for your efforts. It seems that the work has been started there on my post. The status has been changed to STARTED from NEW. I hope that the issue will be resolved as soon as possible.

Thanks
Deep

Brian Burkhalter

Once you see the status change to RESOLVED you should check the CVS mailing
list for the next subsequent entry indicating that a codecLib build has been
integrated. The fix will be available in the first daily build generated after
that.

Brian

On Thu, 3 Aug 2006, jai-imageio@javadesktop.org wrote:

> Hi James,
>
> Thanks for your efforts. It seems that the work has been started there on my post. The status has been changed to STARTED from NEW. I hope that the issue will be resolved as soon as possible.
>
> Thanks
> Deep
> [Message sent by forum member 'saurabhdeep' (saurabhdeep)]
>
> http://forums.java.net/jive/thread.jspa?messageID=139390
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Brian Burkhalter

As a side note, if you don't see something in the javadoc you shouldn't use it
directly in your code. Anything not in the javadoc can change incompatibly at
any time including in daily builds.

Brian

On Thu, 22 Jun 2006, jai-imageio@javadesktop.org wrote:

> Hello,
>
> I am a newbee. Basically I am a coldfusion programmer. For last 20 days I have been working on java. Everything seemed to go smoothly. But I have got stuck while working with Image Libraries.
>
> I need to set the Compression Quality for PNG images & JPEG images. There was no problem while doing it for JPEG images. I used JPEGImageWriteParam for this purpose. It is not possible for me to use ImageWriteParam for this purpose as ImageWriteParam doesn't support PNG format for compression related operations.
>
> I managed to know a little about JAI Image output tools. Now I know it can be done using CLibPNGImageWriteParam. But I am not able to define any parameters using this.
>
> I am stuck at very start where I create an instance of CLibPNGImageWriteParam. This class doesn't seem to be public. That's why it is not letting me create it's object.
>
> I used the following line of code to create the object of JPEGImageWriteParam.
>
> JPEGImageWriteParam jpegparams = new JPEGImageWriteParam( null );
>
> But the similar one I am using for CLibPNGImageWriteParam is not working.
>
> pngparams = new CLibPNGImageWriteParam( null );
>
> Following is the error message that I receive when I try to compile the code.
>
> PresentationTest5.java:61: com.sun.media.imageioimpl.plugins.png.CLibPNGImageWri
> teParam is not public in com.sun.media.imageioimpl.plugins.png; cannot be access
> ed from outside package
> CLibPNGImageWriteParam pngparams;
> ^
>
> PresentationTest5.java:169: com.sun.media.imageioimpl.plugins.png.CLibPNGImageWr
> iteParam is not public in com.sun.media.imageioimpl.plugins.png; cannot be acces
> sed from outside package
> pngparams = new CLibPNGImageWriteParam( null );
> ^
>
> I desperatly need this to work. Any help is appreciated. If needed I can even post the entire Java code. Please let me know If this helps anybody in solving my problem.
>
> Many many Thanks in advance.
> Deep
> [Message sent by forum member 'saurabhdeep' (saurabhdeep)]
>
> http://forums.java.net/jive/thread.jspa?messageID=126196
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

Hello Brian,

I tried a lot but could not find the Java docs for CLibPNGImageWriter, rather I could not find the Java Docs for anything related to com.sun.media.imageioimpl.* !!

Do you have any idea where I can find those?

Looking forward to your response.

Thanks
Deep

Brian Burkhalter

On Thu, 22 Jun 2006, jai-imageio@javadesktop.org wrote:

> I tried a lot but could not find the Java docs for CLibPNGImageWriter, rather I could not find the Java Docs for anything related to com.sun.media.imageioimpl.* !!

You can't find them because they are intentionally not generated.

> Do you have any idea where I can find those?

You can't. The javadocs for JAI Image I/O Tools 1.1-beta are here:

http://download.java.net/media/jai-imageio/javadoc/1.1-beta/index.html

If you are using something from JAI Image I/O Tools which does not figure in
these javadoc pages then you do so at your own risk.

Brian

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

James Cheng

For com.sun.media.imageioimpl.plugins.png.CLibPNGImageWriter,
the following seems to work for me:

BufferedImage image;
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(param.MODE_EXPLICIT);
param.setCompressionQuality(0.1F); // 0.1 ~ 0.9
IIOImage img = new IIOImage(image, null, null);
writer.write(null, img, param);

See this page for more info:
http://download.java.net/media/jai-imageio/javadoc/1.1-latest/overview-s...

-James

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

saurabhdeep
Offline
Joined: 2006-06-22
Points: 0

James,

Thanks for the tip.

In terms of error removal your code works just fine. But it doesn't seem to help me in achieving my goal i.e. Reducing the image size by changing the quality setting.

The sample PNG file that I am using is 122 KB. But after changing the quality parameter to 0.1F the file size reached 2.19 MB, even setting the quality parameter to 0.9F gives the same file size as 0.1F. So, apparantly quality setting is not working and file size is getting increased many a times. On my side I have confirmed that the writer that is in use is CLibPNGImageWriter only.

Any ideas, how should I go on with this?

Thanks
Deep

Robert Engels

Are you certain you are not converting a low bit image (maybe b & w) to full
color in the process?

-----Original Message-----
From: jai-imageio@javadesktop.org [mailto:jai-imageio@javadesktop.org]
Sent: Thursday, June 22, 2006 11:15 PM
To: interest@jai-imageio.dev.java.net
Subject: Re: [JAI-IMAGEIO] Quality setting for PNG images

James,

Thanks for the tip.

In terms of error removal your code works just fine. But it doesn't seem to
help me in achieving my goal i.e. Reducing the image size by changing the
quality setting.

The sample PNG file that I am using is 122 KB. But after changing the
quality parameter to 0.1F the file size reached 2.19 MB, even setting the
quality parameter to 0.9F gives the same file size as 0.1F. So, apparantly
quality setting is not working and file size is getting increased many a
times. On my side I have confirmed that the writer that is in use is
CLibPNGImageWriter only.

Any ideas, how should I go on with this?

Thanks
Deep
[Message sent by forum member 'saurabhdeep' (saurabhdeep)]

http://forums.java.net/jive/thread.jspa?messageID=126321

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net