Skip to main content

Occasional Bad JP2 File Creation with ImageIO

5 replies [Last post]
atait
Offline
Joined: 2006-04-13

Our application, managing 16 bit grayscale images, is occasionally producing a bad (extremely grainy) JP2 file. Internally, the application is managing images as type PlanarImage. The images are always displaying with good detail. When the user is done processing the image, we save out both a thumbnail (.bmp) and a JP2 representing the full image. These bad images are created once in a great while, difficult to replicate. When this does happen, the user is seeing a good image in our display. The thumbnail created from the same image, in line just before creating the JP2, also looks good. However, the JP2, when viewed subsequently is very grainy. Interestingly, the size of these bad JP2s is larger that the JP2s produced from same image when a good JP2 is created. No exceptions are encountered in the associated code.

Java version 1.5_03
jai_imageio 1.0_01

Any direction for fixing is appreciated.

//note, incoming image uses IndexColorModel
public void writeImageAsJPEG2K(File file, PlanarImage image,
int window, int level, boolean invert, File thumbnailFile) {

ImageWriter writer = null;
try {
image = ImageUtilities.windowLevelImage(image, window, level, invert);
BufferedImage bImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_USHORT_GRAY);
Graphics2D g = (Graphics2D)bImage.getGraphics();
g.drawRenderedImage(image.getAsBufferedImage(), null);
g.dispose();
g = null;

if(thumbnailFile != null) {
BufferedImage thumbImage = ImageConversion.resizeImage(bImage, new Dimension(460, 460));
ImageConversion.convertBufferedImage(thumbImage, thumbnailFile, ImageConversion.BMP_OUTPUT);
}

writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg 2000").next();

ImageOutputStream ios = null;
try {
FileOutputStream fileOut = new FileOutputStream(file);
ios = ImageIO.createImageOutputStream(fileOut);
writer.setOutput(ios);

ImageWriteParam iParam = writer.getDefaultWriteParam();

RenderedOp renImage7 = AWTImageDescriptor.create(bImage, null);
IIOImage ioimage = new IIOImage(renImage7, null, null);

writer.write(null, ioimage, iParam);
writer.setOutput(null);
writer.dispose();

ios.flush();
ios.close();

fileOut.flush();
fileOut.close();
} catch (IOException e) {
throw new RuntimeException("Unable to create JPEG2K Image", e);
} finally {
if(writer != null) {
writer.dispose();
writer = null;
}
}
} catch (Exception e) {
_log.error("Exception Writing JPEG2K File", e);
}
}

Reply viewing options

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

Does this problem occur when jai-imageio version 1.1-beta is used? Does it
ever occur when using some other format, e.g., TIFF, in place of JPEG 2000?
Which JPEG 2000 plug-in is being used, the native-based on or the Java one?
If the former, does the problem occur if the latter is used instead?

Brian

On Mon, 24 Jul 2006, jai-imageio@javadesktop.org wrote:

> Our application, managing 16 bit grayscale images, is occasionally producing a bad (extremely grainy) JP2 file. Internally, the application is managing images as type PlanarImage. The images are always displaying with good detail. When the user is done processing the image, we save out both a thumbnail (.bmp) and a JP2 representing the full image. These bad images are created once in a great while, difficult to replicate. When this does happen, the user is seeing a good image in our display. The thumbnail created from the same image, in line just before creating the JP2, also looks good. However, the JP2, when viewed subsequently is very grainy. Interestingly, the size of these bad JP2s is larger that the JP2s produced from same image when a good JP2 is created. No exceptions are encountered in the associated code.
>
> Java version 1.5_03
> jai_imageio 1.0_01
>
> Any direction for fixing is appreciated.
>
>
>
>
> //note, incoming image uses IndexColorModel
> public void writeImageAsJPEG2K(File file, PlanarImage image,
> int window, int level, boolean invert, File thumbnailFile) {
>
> ImageWriter writer = null;
> try {
> image = ImageUtilities.windowLevelImage(image, window, level, invert);
> BufferedImage bImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_USHORT_GRAY);
> Graphics2D g = (Graphics2D)bImage.getGraphics();
> g.drawRenderedImage(image.getAsBufferedImage(), null);
> g.dispose();
> g = null;
>
> if(thumbnailFile != null) {
> BufferedImage thumbImage = ImageConversion.resizeImage(bImage, new Dimension(460, 460));
> ImageConversion.convertBufferedImage(thumbImage, thumbnailFile, ImageConversion.BMP_OUTPUT);
> }
>
> writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg 2000").next();
>
> ImageOutputStream ios = null;
> try {
> FileOutputStream fileOut = new FileOutputStream(file);
> ios = ImageIO.createImageOutputStream(fileOut);
> writer.setOutput(ios);
>
> ImageWriteParam iParam = writer.getDefaultWriteParam();
>
> RenderedOp renImage7 = AWTImageDescriptor.create(bImage, null);
> IIOImage ioimage = new IIOImage(renImage7, null, null);
>
> writer.write(null, ioimage, iParam);
> writer.setOutput(null);
> writer.dispose();
>
> ios.flush();
> ios.close();
>
> fileOut.flush();
> fileOut.close();
> } catch (IOException e) {
> throw new RuntimeException("Unable to create JPEG2K Image", e);
> } finally {
> if(writer != null) {
> writer.dispose();
> writer = null;
> }
> }
> } catch (Exception e) {
> _log.error("Exception Writing JPEG2K File", e);
> }
> }
> [Message sent by forum member 'atait' (atait)]
>
> http://forums.java.net/jive/thread.jspa?messageID=135824
>
> ---------------------------------------------------------------------
> 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

atait
Offline
Joined: 2006-04-13

Thanks Brian

I have not tried swapping in the beta version of imageio. This issue occurs very infrequently, not something I can readily reproduce. Are there related bug fixes in the latest version?

I did try using tiff. This seemed to clear it up. We are moving to standardize on jp2 for the internals of our application, tiff isn't an option.

We are using the Java plug-in and have not tried the native resource. I will look at this option.

Would it help to upload a sample of the bad image we are getting?

Allen

Brian Burkhalter

On Tue, 25 Jul 2006, jai-imageio@javadesktop.org wrote:

> Thanks Brian
>
> I have not tried swapping in the beta version of imageio. This issue occurs very infrequently, not something I can readily reproduce. Are there related bug fixes in the latest version?

There were some fixes but there are still a lot of outstanding problems.
Improving the JPEG 2000 situation is something we are looking to resolve.

> I did try using tiff. This seemed to clear it up. We are moving to standardize on jp2 for the internals of our application, tiff isn't an option.
>
> We are using the Java plug-in and have not tried the native resource. I will look at this option.
>
> Would it help to upload a sample of the bad image we are getting?

It wouldn't hurt. If it's too large I could give you an FTP location off-list.

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

atait
Offline
Joined: 2006-04-13

I sent a sample of the bad JP2 file via email.

Thanks for all of your good work on JAI APIs. Generally, we are having good success using JAI as a foundation for our imaging needs.

Allen

Brian Burkhalter

On Wed, 26 Jul 2006, jai-imageio@javadesktop.org wrote:

> I sent a sample of the bad JP2 file via email.

I also sent a reply separately.

> Thanks for all of your good work on JAI APIs. Generally, we are having good success using JAI as a foundation for our imaging needs.

That's good to hear. Thanks for your positive comments.

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