Skip to main content

Bug (and workaround) when loading lossy JPEG2000 files

8 replies [Last post]
cowwoc
Offline
Joined: 2003-08-24

I converted a JPEG file to JPEG2000 using the LEAD JPEG plugin for Photoshop. I then ran the following test-case on it:

<br />
import com.sun.media.imageio.plugins.jpeg2000.J2KImageReadParam;<br />
import java.awt.Container;<br />
import java.awt.FlowLayout;<br />
import java.awt.image.BufferedImage;<br />
import java.io.File;<br />
import java.io.IOException;<br />
import javax.imageio.ImageIO;<br />
import javax.imageio.ImageReader;<br />
import javax.imageio.stream.ImageInputStream;<br />
import javax.swing.ImageIcon;<br />
import javax.swing.JFrame;<br />
import javax.swing.JLabel;</p>
<p>public class Main<br />
{<br />
  public static void main(String[] args)<br />
  {<br />
    try<br />
    {<br />
      ImageInputStream in = ImageIO.createImageInputStream(new File("test.jp2"));<br />
      ImageReader reader = ImageIO.getImageReaders(in).next();<br />
      reader.setInput(in);</p>
<p>      boolean workaround = false;<br />
      J2KImageReadParam param = new J2KImageReadParam();<br />
      if (workaround)<br />
        param.setResolution(5);<br />
      BufferedImage image = reader.read(0, param);<br />
      JFrame frame = new JFrame();<br />
      Container contentPane = frame.getContentPane();<br />
      contentPane.setLayout(new FlowLayout());<br />
      System.out.println(image.getWidth() + "x" + image.getHeight());<br />
      contentPane.add(new JLabel(new ImageIcon(image)));<br />
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);<br />
      frame.setSize(800, 640);<br />
      frame.setVisible(true);<br />
    }<br />
    catch (IOException e)<br />
    {<br />
      e.printStackTrace();<br />
    }<br />
  }<br />
}<br />

You can download test.jp2 from http://bbs.darktech.org/test.jp2

If "workaround" is false you will notice the image renders "fuzzy". If workaround is set to true the image displays properly (crisp). J2KImageReadParam.setResolution(-1) maps to the fuzzy image whereas J2KImageReadParam.setResolution(5) maps to the full-sized crisp image. The two are supposed to be identical.

Another thing I noticed is that if I compress the file in lossless mode it renders just fine. Can someone please confirm this bug on their end?

Thank you,
Gili

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bokken
Offline
Joined: 2010-05-21

I have also seen this issue. It only occurred when using the native j2k codec (com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib). The pure java implementation (com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader) did not have this issue.

bokken
Offline
Joined: 2010-05-21

Has a bug been logged to the jai project?

robert engels

i think your test case is wrong. If you don't set the resolution it
is going to read the smaller image and scale it to the required size
- thus the fuzziness.

A jpeg2000 contains multiple high-quality images, but of different
sizes.

On Jun 30, 2008, at 8:20 PM, jai-imageio@javadesktop.org wrote:

> I converted a JPEG file to JPEG2000 using the LEAD JPEG plugin for
> Photoshop. I then ran the following test-case on it:
>
> [code]
> import com.sun.media.imageio.plugins.jpeg2000.J2KImageReadParam;
> import java.awt.Container;
> import java.awt.FlowLayout;
> import java.awt.image.BufferedImage;
> import java.io.File;
> import java.io.IOException;
> import javax.imageio.ImageIO;
> import javax.imageio.ImageReader;
> import javax.imageio.stream.ImageInputStream;
> import javax.swing.ImageIcon;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
>
> public class Main
> {
> public static void main(String[] args)
> {
> try
> {
> ImageInputStream in = ImageIO.createImageInputStream(new File
> ("test.jp2"));
> ImageReader reader = ImageIO.getImageReaders(in).next();
> reader.setInput(in);
>
> boolean workaround = false;
> J2KImageReadParam param = new J2KImageReadParam();
> if (workaround)
> param.setResolution(5);
> BufferedImage image = reader.read(0, param);
> JFrame frame = new JFrame();
> Container contentPane = frame.getContentPane();
> contentPane.setLayout(new FlowLayout());
> System.out.println(image.getWidth() + "x" + image.getHeight());
> contentPane.add(new JLabel(new ImageIcon(image)));
> frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
> frame.setSize(800, 640);
> frame.setVisible(true);
> }
> catch (IOException e)
> {
> e.printStackTrace();
> }
> }
> }
> [/code]
>
> You can download test.jp2 from http://bbs.darktech.org/test.jp2
>
> If "workaround" is false you will notice the image renders "fuzzy".
> If workaround is set to true the image displays properly (crisp).
> [code]J2KImageReadParam.setResolution(-1)[/code] maps to the fuzzy
> image whereas [code]J2KImageReadParam.setResolution(5)[/code] maps
> to the full-sized crisp image. The two are supposed to be identical.
>
> Another thing I noticed is that if I compress the file in lossless
> mode it renders just fine. Can someone please confirm this bug on
> their end?
>
> Thank you,
> Gili
> [Message sent by forum member 'cowwoc' (cowwoc)]
>
> http://forums.java.net/jive/thread.jspa?messageID=283521
>
> ---------------------------------------------------------------------
> 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

cowwoc
Offline
Joined: 2003-08-24

> i think your test case is wrong. If you don't set the
> resolution it
> is going to read the smaller image and scale it to
> the required size
> - thus the fuzziness.
>
> A jpeg2000 contains multiple high-quality images, but
> of different
> sizes.

I guess we need to clarify the Javadoc. It says: "The default value, -1, means to use the resolution level at encoding." What does that actually mean?

As far as I understand it, the main resolution of a JPEG2000 file is the highest resolution, and all other ones are bi-products of the algorithm. As such, if the default resolution is anything except the highest one then I personally find it non-intuitive. Remember, when I convert an image from another file format to JPEG2000 what I am interested in storing is the highest resolution, and the encoder generates the lower resolutions completely as a bi-product.

Gili

robert engels

Then don't set the option...

On Jul 1, 2008, at 11:49 AM, jai-imageio@javadesktop.org wrote:

>> i think your test case is wrong. If you don't set the
>> resolution it
>> is going to read the smaller image and scale it to
>> the required size
>> - thus the fuzziness.
>>
>> A jpeg2000 contains multiple high-quality images, but
>> of different
>> sizes.
>
> I guess we need to clarify the Javadoc. It says: "The default
> value, -1, means to use the resolution level at encoding." What
> does that actually mean?
>
> As far as I understand it, the main resolution of a JPEG2000 file
> is the highest resolution, and all other ones are bi-products of
> the algorithm. As such, if the default resolution is anything
> except the highest one then I personally find it non-intuitive.
> Remember, when I convert an image from another file format to
> JPEG2000 what I am interested in storing is the highest resolution,
> and the encoder generates the lower resolutions completely as a bi-
> product.
>
> Gili
> [Message sent by forum member 'cowwoc' (cowwoc)]
>
> http://forums.java.net/jive/thread.jspa?messageID=283708
>
> ---------------------------------------------------------------------
> 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

cowwoc
Offline
Joined: 2003-08-24

> Then don't set the option...

... but I don't... What I'm saying is that if I set the level to -1 or if I use the default ImageReadParam I get the same fuzzy output. The only way I found to overcome this is to use a J2KImageReadParam with a resolution of 5.

Gili

robert engels

works fine for me. can put post the full code and sample images
somewhere?

On Jul 1, 2008, at 11:59 AM, jai-imageio@javadesktop.org wrote:

>> Then don't set the option...
>
> ... but I don't... What I'm saying is that if I set the level to -1
> or if I use the default ImageReadParam I get the same fuzzy output.
> The only way I found to overcome this is to use a J2KImageReadParam
> with a resolution of 5.
>
> Gili
> [Message sent by forum member 'cowwoc' (cowwoc)]
>
> http://forums.java.net/jive/thread.jspa?messageID=283712
>
> ---------------------------------------------------------------------
> 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

cowwoc
Offline
Joined: 2003-08-24